/**
 * 
 */
package com.example.android_is2.ListenerMap;

import java.util.List;

import android.location.Location;
import android.util.Log;

import com.example.android_is2.MainActivity;
import com.example.android_is2.MapItemizedOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * @author KAIN-PC
 *
 */
public class LocationListenerMapHere extends LocationListenerMap {

  public static final String MAP_HERE = "I'm HERE";
  
  public LocationListenerMapHere(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedoverlay) {
    // TODO Auto-generated constructor stub
    super(mapView,overlays,itemizedoverlay);
  }

  /* (non-Javadoc)
   * @see com.example.android_is2.LocationListenerMap#onLocationChanged(android.location.Location)
   */
  @Override
  public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    getMapView().invalidate();
    // Aggiorna la location
    if (location != null) {
      int geoLat = (int) (location.getLatitude() * 1E6);
      int geoLng = (int) (location.getLongitude() * 1E6);

      Log.i(MainActivity.TAG + getClass(), "Lat: " + geoLat + " - Lng: " + geoLng);
      GeoPoint point = new GeoPoint(geoLat, geoLng);
      if (point != null) {
        // Rimozione Overlay precedenti per aggiornamento
        getItemizedoverlay_my().clearOverlay();

        getMapcontroller().animateTo(point);
        OverlayItem overlayitem = new OverlayItem(point, MAP_HERE, "Lat: "
            + Double.toString(getRound(location.getLatitude(), 5)) + "\nLng: "
            + Double.toString(getRound(location.getLongitude(), 5)));

        getItemizedoverlay_my().addOverlay(overlayitem);
        getOverlays().add(getItemizedoverlay_my());

        Log.i(MainActivity.TAG + getClass(),
            "Lat: " + (Double.toString(getRound(location.getLatitude(), 5))));
        Log.i(MainActivity.TAG + getClass(),
            "Long: " + (Double.toString(getRound(location.getLongitude(), 5))));
        Log.i(MainActivity.TAG + getClass(),
            "Spd: " + (Double.toString(getRound(location.getSpeed() * 3.6, 1)) + " km/h"));
        Log.i(MainActivity.TAG + getClass(),
            "Alt: " + (Integer.toString((int) location.getAltitude()) + " m"));
        Log.i(MainActivity.TAG + getClass(),
            "Acc: " + (Integer.toString((int) location.getAccuracy()) + " m"));
      }
    }
  }

}
